﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using com.nm;
using com.nm.Attributes;
using com.nm.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WeatherAPI.Helper
{
    public class WeatherHelper
    {
        public enum BigDataType
        {
            /// <summary>
            /// 预报
            /// </summary>
            forecast = 1001001,

            /// <summary>
            /// 实况
            /// </summary>
            observe = 1001002
        }

        /// <summary>
        /// 小类数据中文对照
        /// </summary>
        /// <returns></returns>
        public static List<DataTypeModel> InitDataType()
        {
            var list = new List<DataTypeModel>
            {
                new DataTypeModel() {DataType = "1001001", ChinaeseName = "白天天气", SmallCode = "001"},
                new DataTypeModel() {DataType = "1001001", ChinaeseName = "晚上天气", SmallCode = "002"},
                new DataTypeModel() {DataType = "1001001", ChinaeseName = "白天温度", SmallCode = "003"},
                new DataTypeModel() {DataType = "1001001", ChinaeseName = "晚上温度", SmallCode = "004"},
                new DataTypeModel() {DataType = "1001001", ChinaeseName = "白天风力", SmallCode = "005"},
                new DataTypeModel() {DataType = "1001001", ChinaeseName = "晚上风力", SmallCode = "006"},
                new DataTypeModel() {DataType = "1001001", ChinaeseName = "白天方向", SmallCode = "007"},
                new DataTypeModel() {DataType = "1001001", ChinaeseName = "晚上方向", SmallCode = "008"},
                new DataTypeModel() {DataType = "1001002", ChinaeseName = "实况发布时间", SmallCode = "000"},
                new DataTypeModel() {DataType = "1001002", ChinaeseName = "当前温度(摄氏度)", SmallCode = "002"},
                new DataTypeModel() {DataType = "1001002", ChinaeseName = "当前风力(级)", SmallCode = "003"},
                new DataTypeModel() {DataType = "1001002", ChinaeseName = "当前风向", SmallCode = "004"},
                new DataTypeModel() {DataType = "1001002", ChinaeseName = "当前湿度(%)", SmallCode = "005"},
                new DataTypeModel() {DataType = "1001002", ChinaeseName = "当前降水量(毫米)", SmallCode = "006"},
                new DataTypeModel() {DataType = "1001002", ChinaeseName = "当前气压(百帕)", SmallCode = "007"}
            };
            return list;
        }
        /// <summary>
        /// 天气现象编码表
        /// </summary>
        public static List<WindDirectionModel> WindDirection()
        {
            var list = new List<WindDirectionModel>
            {
                new WindDirectionModel() {keys = "00", values = "晴",EnName = "Sunny"},
                new WindDirectionModel() {keys = "01", values = "多云",EnName = "Cloudy"},
                new WindDirectionModel() {keys = "02", values = "阴",EnName = "Overcast"},
                new WindDirectionModel() {keys = "03", values = "阵雨",EnName = "Shower"},
                new WindDirectionModel() {keys = "04", values = "雷阵雨",EnName = "Thundershower"},
                new WindDirectionModel() {keys = "05", values = "雷阵雨伴有冰雹",EnName = "ThundershowerWithHail"},
                new WindDirectionModel() {keys = "06", values = "雨夹雪",EnName = "Sleet"},
                new WindDirectionModel() {keys = "07", values = "小雨",EnName = "LightRain"},
                new WindDirectionModel() {keys = "08", values = "中雨",EnName = "ModerateRain"},
                new WindDirectionModel() {keys = "09", values = "大雨",EnName = "HeavyRain"},
                new WindDirectionModel() {keys = "10", values = "暴雨",EnName = "Storm"},
                new WindDirectionModel() {keys = "11", values = "大暴雨",EnName = "HeavyStorm"},
                new WindDirectionModel() {keys = "12", values = "特大暴雨",EnName = "SevereStorm"},
                new WindDirectionModel() {keys = "13", values = "阵雪",EnName = "SnowFlurry"},
                new WindDirectionModel() {keys = "14", values = "小雪",EnName = "LightSnow"},
                new WindDirectionModel() {keys = "15", values = "中雪",EnName = "ModerateSnow"},
                new WindDirectionModel() {keys = "16", values = "大雪",EnName = "HeavySnow"},
                new WindDirectionModel() {keys = "17", values = "暴雪",EnName = "Snowstorm"},
                new WindDirectionModel() {keys = "18", values = "雾",EnName = "Foggy"},
                new WindDirectionModel() {keys = "19", values = "冻雨",EnName = "IceRain"},
                new WindDirectionModel() {keys = "20", values = "沙尘暴",EnName = "Duststorm"},
                new WindDirectionModel() {keys = "21", values = "小到中雨",EnName = "LightToModerateRain"},
                new WindDirectionModel() {keys = "22", values = "中到大雨",EnName = "ModerateToHeavyRain"},
                new WindDirectionModel() {keys = "23", values = "大到暴雨",EnName = "HeavyRainToStorm"},
                new WindDirectionModel() {keys = "24", values = "暴雨到大暴雨",EnName = "StormToHeavyStorm"},
                new WindDirectionModel() {keys = "25", values = "大暴雨到特大暴雨",EnName = "HeavyToSevereStorm"},
                new WindDirectionModel() {keys = "26", values = "小到中雪",EnName = "LightToModerateSnow"},
                new WindDirectionModel() {keys = "27", values = "中到大雪",EnName = "ModerateToHeavySnow"},
                new WindDirectionModel() {keys = "28", values = "大到暴雪",EnName = "HeavySnowToSnowstorm"},
                new WindDirectionModel() {keys = "29", values = "浮尘",EnName = "Dust"},
                new WindDirectionModel() {keys = "30", values = "扬沙",EnName = "Sand"},
                new WindDirectionModel() {keys = "31", values = "强沙尘暴",EnName = "Sandstorm"},
                new WindDirectionModel() {keys = "53", values = "霾",EnName = "Haze"},
                new WindDirectionModel() {keys = "99", values = "无",EnName = "Unknown"}
            };
            return list;
        }

        /// <summary>
        /// 返回日期几
        /// </summary>
        /// <returns></returns>
        public static string DayOfWeek(DateTime date)
        {
            switch (date.DayOfWeek.ToString("D"))
            {
                case "0":
                    return "星期日 ";
                case "1":
                    return "星期一 ";
                case "2":
                    return "星期二 ";
                case "3":
                    return "星期三 ";
                case "4":
                    return "星期四 ";
                case "5":
                    return "星期五 ";
                case "6":
                    return "星期六 ";
            }
            return null;
        }
        
    }

    public class IpHelper
    {


        /// <summary>
        /// 获取客户端IP地址（无视代理）
        /// </summary>
        /// <returns>若失败则返回回送地址</returns>
        public static string GetHostAddress()
        {
            var userHostAddress = HttpContext.Current.Request.UserHostAddress;

            if (string.IsNullOrEmpty(userHostAddress))
            {
                userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            //最后判断获取是否成功，并检查IP地址的格式（检查其格式非常重要）
            if (!string.IsNullOrEmpty(userHostAddress) && IsIP(userHostAddress))
            {
                Log.WriteLines("天气接口获取IP地址："+userHostAddress);
                return userHostAddress;
            }
            return "127.0.0.1";
           // return "183.60.175.74";
        }

        /// <summary>
        /// 检查IP地址格式
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIP(string ip)
        {
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
        /// <summary>
        /// 通过IP获取城市
        /// </summary>
        /// <returns></returns>
        public static string IPGetCity()
        {
            try
            {
                var myWebClient = new WebClient {Credentials = CredentialCache.DefaultCredentials};
                //获取或设置用于向Internet资源的请求进行身份验证的网络凭据  
                var pageData = myWebClient.DownloadData("http://ip.taobao.com/service/getIpInfo.php?ip="+ GetHostAddress()); //从指定网站下载数据  
                var pageHtml = Encoding.Default.GetString(pageData);
                var dic = pageHtml.FromJson<Dictionary<string, object>>();
                if (dic["code"].ToString() == "0")
                {
                    var dicData = (JObject) JsonConvert.DeserializeObject(dic["data"].ToJson());
                    foreach (var item in dicData)
                    {
                        if (item.Key == "city")
                            return item.Value.ToString();
                    }
                }
            }
            catch (WebException webEx)
            {
                Log.Error("通过IP获取城市失败："+webEx);
            }
            return null;
        }
    }

    [Description("具体天气信息")]
    public class DataTypeModel
    {
        [ParamDescription("大类数据类型")]
        public string DataType { get; set; }

        [ParamDescription("中文名")]
        public string ChinaeseName { get; set; }

        [ParamDescription("小类数据类型")]
        public string SmallCode { get; set; }
    }

    [Description("气象结果")]
    public class WindDirectionModel
    {
        [ParamDescription("关键字")]
        public string keys { get; set; }

        [ParamDescription("对应值")]
        public string values { get; set; }

        [ParamDescription("英文名称")]
        public  string EnName { get; set; }

    }
}
